package com.servlet;

import com.alibaba.fastjson.JSON;
import com.dao.CardDao;
import com.dao.Impl.CardDaoImpl;
import com.dao.Impl.UserDaoImpl;
import com.dao.UserDao;
import com.entity.Card;
import com.entity.JoinQuery;
import com.entity.PageBean;
import com.entity.User;
import com.util.TransBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * 会员列表查询
 */
@WebServlet(value = "/userServlet/*")
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String url = request.getRequestURI();
        String methon = url.substring(url.lastIndexOf("/")+1);
        String target = null;
        switch (methon){
            case "findMember":
                target=findMember(request,response);
                break;
            case "findHunt":
                target=findHunt(request,response);
                break;
            case "findPageBean":
                target=findPageBean(request,response);
                break;
            case "deleteID":
                target=deleteID(request,response);
                break;
            case "alterID":
                target=alterID(request,response);
                break;
            case "stateAlter":
                target=stateAlter(request,response);
                break;
            case "cartogram":
                target=cartogram(request,response);
                break;
            case "findMemberDetails":
                target=findMemberDetails(request,response);
                break;
            case "insertUser":
                target=insertUser(request,response);
                break;

            default:break;
        }
        if (target!=null){
            request.getRequestDispatcher(target).forward(request, response);
        }

    }

    /**
     * 会员列表查询
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    private String findMember(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserDao udao=new UserDaoImpl();
        List<User> allMember = udao.findAllMember();
        request.setAttribute("user",allMember);
        CardDao cdao=new CardDaoImpl();
        List<Card> amout = cdao.findAmout();
        request.setAttribute("card",amout);
        return "/member/list.jsp";
    }

    /**
     * 删除会员列表信息
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    private String deleteID(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserDao udao=new UserDaoImpl();
        int id= Integer.parseInt(request.getParameter("id"));
        udao.deleteId(id);
        return "/member/list.jsp";
    }

    /**
     * 修改会员列表数据
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    private String alterID(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserDao udao=new UserDaoImpl();
        User user=new User();
        user.setUserId(Integer.valueOf(request.getParameter("userId")));
        user.setUserName(request.getParameter("userName"));
        user.setPhone(request.getParameter("phone"));
        user.setIdCard(request.getParameter("idCard"));
        user.setAddress(request.getParameter("address"));
        user.setStatus(Integer.valueOf(request.getParameter("status")));
        udao.alter(user);
        List<User> one = udao.findOne(user.getUserId());
        ajaxOutput(response, JSON.toJSONString(one));
        return null;
    }


    /**
     * 根据搜索查询
     * 会员编号和名字同时传值时根据会员编号查询
     *
     */
    private String findHunt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userId= request.getParameter("userId");
        String userName= request.getParameter("userName");
        if (userId!=null){
            UserDao udao=new UserDaoImpl();
            List<User> allMember=udao.findOne(Integer.valueOf(userId));
            request.setAttribute("user",allMember);
            CardDao cdao=new CardDaoImpl();
            List<Card> amout = cdao.findAmout();
            request.setAttribute("card",amout);
            return "/member/list.jsp";
        }else if (userName!=null){
            return null;
        }
        return null;
    }



    /**
     * 分页查询
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected String findPageBean(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PageBean<User> pb=new PageBean<>();
        String num=request.getParameter("pageNo");
        String size=request.getParameter("pageSize");
        String[] cond=request.getParameterValues("condition[]");
        pb.setFactor(cond);//查询条件数组
        pb.setPageSize(Integer.parseInt(size));//每页行数
        pb.setPageNo(Integer.parseInt(num));//当前页数,默认为1
        UserDao dao=new UserDaoImpl();
        dao.findPageBean(pb);//查询出的数据
        CardDao cdao=new CardDaoImpl();
        List<Card> amout = cdao.findAll();
        JoinQuery join=new JoinQuery();
        join.setCards(amout);
        join.setPageBeans(pb);
        ajaxOutput(response, JSON.toJSONString(join));
        return null;

    }

    /**
     * 会员刷卡中会员详情的分页查询
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */

    protected String findMemberDetails(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PageBean<User> pb=new PageBean<>();
        String num=request.getParameter("pageNo");
        String size=request.getParameter("pageSize");
        String[] cond=request.getParameterValues("condition[]");
        pb.setFactor(cond);//查询条件数组
        pb.setPageSize(Integer.parseInt(size));//每页行数
        pb.setPageNo(Integer.parseInt(num));//当前页数,默认为1
        UserDao dao=new UserDaoImpl();
        dao.findMemberDetails(pb);
        ajaxOutput(response, JSON.toJSONString(pb));
        return null;

    }


    /**
     * 向前面传输json数据
     * @param response
     * @param jsonstr
     */
    private void ajaxOutput(HttpServletResponse response, String jsonstr) {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.println(jsonstr);
        out.close();
    }
    private String stateAlter(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Card card=new Card();
        card.setCardId(Integer.valueOf(request.getParameter("userId")));
        card.setStatus(Integer.valueOf(request.getParameter("status")));
        CardDao dao=new CardDaoImpl();
        dao.modifyStatus(card);
        return null;

    }
    /**
     * 统计图的servlet
     *统计图英文叫 ：cartogram
     */
    private String cartogram(HttpServletRequest request,HttpServletResponse response){
        UserDao userDao =new UserDaoImpl();
        Map map = userDao.findsum();
        for (int i = 1; i <= 12 ; i++) {
            if (map.get(i) == null){
                map.put(i,0);
            }
        }
        request.setAttribute("umap",JSON.toJSONString(map));
        return "/statistics/membershipGrowth.jsp";
    }

    /**
     * 会员登记添加信息
     * @param request
     * @param response
     * @return
     */
    private String insertUser(HttpServletRequest request,HttpServletResponse response){
        User user=new User();
        TransBean.populate(user,request.getParameterMap());
        UserDao dao=new UserDaoImpl();
        user.setUserId(dao.findUserId()+1);
        dao.insertUser(user);
        return null;
    }

}
